package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Shipping;
import com.mmall.pojo.User;
import com.mmall.service.IShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author cg.hu
 * @version 1.0.0
 * @date 2021-09-28 10:06
 * @description 收货地址相关
 * @since 1.0.0
 */
@Controller
@RequestMapping("/shipping/")
public class ShippingController {

    @Autowired
    private IShippingService iShippingService;

    /**
     * @author cg.hu
     * @version 1.0.0
     * @date 2021-09-28 10:12
     * @description    新增收货地址
     **/
    @ResponseBody
    @RequestMapping(value = "add.do", method = RequestMethod.POST)
    public ServerResponse add(HttpSession session, Shipping shipping) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (null == user) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "未登录");
        }
        return iShippingService.add(user.getId(), shipping);
    }

    /**
     * @author cg.hu
     * @version 1.0.0
     * @date 2021-09-28 10:44
     * @description    删除收货信息
     **/
    @ResponseBody
    @RequestMapping(value = "del.do", method = RequestMethod.POST)
    public ServerResponse del(HttpSession session, Integer shippingId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (null == user) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "未登录");
        }
        return iShippingService.del(user.getId(), shippingId);
    }

    /**
     * @author cg.hu
     * @version 1.0.0
     * @date 2021-09-28 10:52
     * @description    更新收货信息
     **/
    @ResponseBody
    @RequestMapping(value = "update.do", method = RequestMethod.POST)
    public ServerResponse update(HttpSession session, Shipping shipping) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (null == user) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "未登录");
        }
        return iShippingService.update(user.getId(), shipping);
    }

    /**
     * @author cg.hu
     * @version 1.0.0
     * @date 2021-09-28 11:11
     * @description    查寻收货信息详情
     **/
    @ResponseBody
    @RequestMapping(value = "select.do", method = RequestMethod.POST)
    public ServerResponse select(HttpSession session, Integer shippingId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (null == user) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "未登录");
        }
        return iShippingService.select(user.getId(), shippingId);
    }

    /**
     * @author cg.hu
     * @version 1.0.0
     * @date 2021-09-28 11:27
     * @description    分页获取列表数据
     **/
    @ResponseBody
    @RequestMapping(value = "list.do", method = RequestMethod.POST)
    public ServerResponse list(HttpSession session,
                               @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize
    ) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (null == user) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "未登录");
        }
        return iShippingService.list(user.getId(), pageNum, pageSize);
    }
}
